package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.mongo.UserLocation;
import com.itheima.service.mongo.UserLocationService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.connection.RedisGeoCommands;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserLocationServiceImpl implements UserLocationService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void uploadUserLocation(Long userId, Double longitude, Double latitude, String addrStr) {
        // 1.先查询此用户的坐标数据
        // 1-1 构建查询对象
        Query query = new Query(Criteria.where("userId").is(userId));
        // 1-2 根据条件查询
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);

        if (userLocation == null) {// 2.新增
            userLocation = new UserLocation();
            userLocation.setUserId(userId); // 用户id
            GeoJsonPoint geoJsonPoint = new GeoJsonPoint(longitude, latitude); // 坐标点
            userLocation.setLocation(geoJsonPoint);
            userLocation.setAddress(addrStr); // 位置描述
            userLocation.setCreated(System.currentTimeMillis()); // 创建时间
            userLocation.setUpdated(System.currentTimeMillis()); // 修改时间
            userLocation.setLastUpdated(System.currentTimeMillis()); // 上次修改时间
            mongoTemplate.save(userLocation);
        } else {// 3.修改
            GeoJsonPoint geoJsonPoint = new GeoJsonPoint(longitude, latitude); // 坐标点
            userLocation.setLocation(geoJsonPoint);
            userLocation.setAddress(addrStr); // 位置描述
            userLocation.setLastUpdated(userLocation.getUpdated()); // 上次修改时间
            userLocation.setUpdated(System.currentTimeMillis());// 本次修改时间
            mongoTemplate.save(userLocation);
        }
    }


    // 搜附近
    @Override
    public List<Long> searchNearUser(Long userId, Integer distance) {
        // 1.查自己位置作为圆心
        UserLocation userLocation = mongoTemplate.findOne(new Query(Criteria.where("userId").is(userId)), UserLocation.class);
        // 2.指定半径范围
        Distance dis = new Distance(distance / 1000, RedisGeoCommands.DistanceUnit.KILOMETERS);
        // 3.画圆
        Circle circle = new Circle(userLocation.getLocation(), dis);
        // 4.在圆内搜索
        Query query = new Query(Criteria.where("location").withinSphere(circle));
        List<UserLocation> userLocationList = mongoTemplate.find(query, UserLocation.class);
        // 5.返回附近的人id
        List<Long> nearUserIdList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(userLocationList)) {
            for (UserLocation location : userLocationList) {
                nearUserIdList.add(location.getUserId());
            }
        }
        return nearUserIdList;
    }
}
